import { connect } from 'dva'

import { deleteCompanionCompanyApi } from '@/api/pcs/pcsCompanionCompany'
import AekButton from '@/components/AekButton'
import AekLinkButton from '@/components/AekLinkButton'
import AekTable from '@/components/AekTable'
import PageLayout from '@/components/PageLayout'
import SearchForm from '@/components/SearchForm'
import { namespace } from '@/models/pcs/pcsCompanionCompany'
import { getPagination, message, modal } from '@/utils/antdHelper'
import { getBasicFn } from '@/utils/dvaUtil/index'
import { requestWithTry } from '@/utils/request'

import EditModal from './components/EditModal'

/** 陪护 - 陪护公司管理 */
const Index = ({ modelState, loading }) => {
  const { pageList, pagination, searchParams } = modelState
  const { toAction, getLoading } = getBasicFn({ namespace, loading })

  const add = async () => {
    await EditModal.PromiseThen()
    message.success('新增成功')
    toAction('getPageList')
  }

  const edit = async row => {
    await EditModal.PromiseThen({
      row,
    })
    message.success('修改成功')
    toAction('getPageList')
  }

  const remove = async ({ id, name }) => {
    modal.confirm({
      title: '删除',
      content: `确认删除陪护公司【${name}】？`,
      onOk: async () => {
        await requestWithTry(deleteCompanionCompanyApi, { id })
        message.success('删除成功')
        if ((pagination.current - 1) * pagination.pageSize === pagination.total - 1) {
          toAction('getPageList', { current: pagination.current - 1 || 1 })
        } else {
          toAction('getPageList')
        }
      },
    })
  }

  /** @type {import("@/components/SearchForm").SearchFormProps} */
  const searchProps = {
    searchParams,
    formData: [
      {
        field: 'name',
        label: '公司名称',
        component: {
          name: 'Input',
        },
      },
    ],
    onSearch: values => {
      toAction({ searchParams: values })
      toAction({ pagination: { current: 1 } }, true)
      toAction('getPageList')
    },
  }

  /** @type {import("@/components/AekTable").AekTableProps} */
  const tableProps = {
    configKey: namespace,
    rowKey: 'id',
    loading: getLoading('getPageList'),
    dataSource: pageList,
    pagination: getPagination(pagination, (current, pageSize) => {
      toAction('getPageList', { current, pageSize })
    }),
    columns: [
      { dataIndex: 'name', title: '公司名称' },
      { dataIndex: 'serviceProvidingCityName', title: '服务城市' },
      { dataIndex: 'serviceProvidingHospitalName', title: '服务医院' },
      { dataIndex: 'remark', title: '备注', ellipsis: true },
      {
        key: 'operate',
        title: '操作',
        fixed: 'right',
        width: 100,
        className: 'text-center!',
        render: (_, row) => (
          <>
            <AekLinkButton permissionKey={`${namespace}:update`} onClick={() => edit(row)}>
              编辑
            </AekLinkButton>
            <AekLinkButton permissionKey={`${namespace}:delete`} danger onClick={() => remove(row)}>
              删除
            </AekLinkButton>
          </>
        ),
      },
    ],
  }

  return (
    <PageLayout
      extraSuffix={
        <AekButton permissionKey={`${namespace}:create`} type='primary' onClick={add}>
          新建
        </AekButton>
      }
    >
      <SearchForm {...searchProps} />
      <AekTable {...tableProps} />
    </PageLayout>
  )
}

export default connect(store => ({
  modelState: store[namespace],
  loading: store.loading,
}))(Index)
